package com.amumu.drama.common.commpont.transaction;

import com.amumu.drama.common.base.annonation.OnContextInited;
import com.amumu.drama.common.commpont.SpringContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author sunzhuang
 * @date 2024/10/22 11:43
 * @slogan 天下风云出我辈, 一入代码岁月催
 * @desc todo
 */

@Component
public class TransactionHelper {

    public static TransactionHelper INSTANCE;

    @OnContextInited
    public void init() {
        INSTANCE = SpringContext.getBean(TransactionHelper.class);
    }


    /** 执行本地事务（同一数据源） */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    public void run(TransactionRunnable target) throws Throwable {
        target.run();
    }

    /** 执行本地事务（跨多数据源，不推荐使用，应避免出现这样的业务逻辑）
     @DSTransactional public void runOnMultiDataSource(TransactionRunnable target) throws Throwable {
     target.run();
     }
     */
}
